package com.project.service.impl;

import com.project.bean.RoomBean;
import com.project.mapper.IRoomMapper;
import com.project.service.IRoomService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class RoomServiceImpl extends BaseService implements IRoomService {
    @Override
    public void add(RoomBean room) {
        //获取会话对象
        SqlSession session = this.getSession();
        //回去mapper接口实现类对象
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        //调用mapper添加方法
        mapper.add(room);
        //提交事务
        session.commit();
        //关闭会话
        session.close();

    }

    @Override
    public void del(Integer id) {
        //获取会话对象
        SqlSession session = this.getSession();
        //回去mapper接口实现类对象
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        //调用mapper方法
        mapper.del(id);
        //提交事务
        session.commit();
        //关闭会话
        session.close();
    }

    @Override
    public void update(Integer id, Integer price) {
        //获取会话对象
        SqlSession session = this.getSession();
        //回去mapper接口实现类对象
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        //调用mapper方法
        mapper.update(id, price);
        //提交事务
        session.commit();
        //关闭会话
        session.close();
    }

    @Override
    public List<RoomBean> findAll() {
        //获取会话对象
        SqlSession session = this.getSession();
        //回去mapper接口实现类对象
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        //调用mapper方法
        List<RoomBean> list = mapper.findAll();
        //关闭会话
        session.close();
        return list;
    }

    @Override
    public RoomBean findById(Integer id) {
        //获取会话对象
        SqlSession session = this.getSession();
        //回去mapper接口实现类对象
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        //调用mapper方法
        RoomBean room = mapper.findById(id);
        //关闭会话
        session.close();
        return room;
    }
}
